/******************************************************************************************************
Copyright (C) 2008 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/
// This example function demonstrates how to apply some processing to a set of spectral files and to save the result in the different file formats.
function import_process_save_loop()
{
	var dirName = "c:/spectra"; // Source directory with spectra to be processed.
	var resultDirName = dirName; // Destination directory where processed spectra should be put.
	var procName = "c:/temp/proc.mnp"; // Processing file. It must be created in the Processing Template dialog of MestReNova.
	var fileMask = "fid"; // This mask specifies files to be processed. 
	var dirMask = "*.fid"; // This mask specifies directories to be scanned for spectra.

	var dir = new Dir(dirName);
	if (!dir.exists)
	{
		MessageBox.critical("Directory " + dirName + " does not exist.", MessageBox.Ok);
		return;
	}
	dir.cdUp();
	
	// The below function getMaskFiles is located in the files.qs script. 
	var files = getMaskFiles(dirName, dir.absPath, fileMask, dirMask, true); 
	for(var i = 0; i < files.length; i++)
	{
		var dw = new DocumentWindow(Application.mainWindow.newWindow()); // Create new document window
		if (serialization.open(files[i]))
		{
			nmr.process(procName);
			var toReplace = new RegExp("[/|:]", "g"); 
			fileName = resultDirName + "/" + files[i].replace(toReplace, "_"); // Generate a filename for processed spectrum. 
			serialization.save(fileName+".mnova", "mnova");	// MestReNova file format
			serialization.save(fileName+".pdf", "pdf"); // Adobe PDF file format
			serialization.save(fileName+".txt", "ascii"); // ASCII
			print(fileName); // Prints file name in the debug window
		}
		dw.close(); // Close document window of processed spectrum
	}
}
